package com.earthchen.demo.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.earthchen.demo.dao.SysResourceDao;
import com.earthchen.demo.domain.SysResource;
import com.earthchen.demo.enums.ResourceTypeEnum;
import com.earthchen.demo.service.SysResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author earthchen
 * @date 2018/9/4
 **/
@Service
public class SysResourceServiceImpl extends ServiceImpl<SysResourceDao, SysResource>
        implements SysResourceService {

    @Autowired
    private SysResourceDao sysResourceDao;

    @Override
    public List<SysResource> getAuthorityApisByRoleId(long roleId) {
        return sysResourceDao.selectApisByRoleId(roleId);
    }

    @Override
    public List<SysResource> getAuthorityMenusByRoleId(long roleId) {
        return sysResourceDao.selectMenusByRoleId(roleId);
    }

    @Override
    public List<SysResource> getAuthorityMenusByUserId(Long userId) {
        return sysResourceDao.getAuthorityMenusByUserId(userId);
    }

    @Override
    public List<SysResource> getMenus() {
        return selectList(new EntityWrapper<SysResource>()
                .where("type", ResourceTypeEnum.MENU.getCode()));
    }

    @Override
    public Boolean deleteMenuByMenuId(Long menuId) {
        return deleteById(menuId);
    }

    @Override
    public List<SysResource> getApiTeamList() {
        return selectList(new EntityWrapper<SysResource>()
                .where("type", ResourceTypeEnum.RESOURCE_TYPE.getCode()));
    }

    @Override
    public List<SysResource> getApiList() {
        return selectList(new EntityWrapper<SysResource>()
                .where("type", ResourceTypeEnum.ELEMENT.getCode()));
    }

    @Override
    public List<SysResource> getApiListByTeamId(Long teamId) {
        return selectList(new EntityWrapper<SysResource>()
                .where("parent_id={0} and type in {1}", teamId,
                        "(" + ResourceTypeEnum.ELEMENT.getCode() + "," + ResourceTypeEnum.RESOURCE_TYPE.getCode() + ")"));
    }
}
